package com.example.ffmpegclipvideomodule.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.ffmpegclipvideomodule.R;
import com.example.ffmpegclipvideomodule.base.BaseActivity;
import com.example.ffmpegclipvideomodule.databinding.ActivityClipVideoBinding;
import com.example.ffmpegclipvideomodule.listener.VideoChangeState;
import com.example.ffmpegclipvideomodule.listener.VideoTrimmerListener;
import com.example.ffmpegclipvideomodule.util.StorageUtil;
import com.example.ffmpegclipvideomodule.util.VideoTrimmerUtil;


public class ClipVideoActivity extends BaseActivity<ActivityClipVideoBinding> implements VideoTrimmerListener {

    //获取得到路径
    private String path;
    private ProgressDialog mProgressDialog;
    private VideoTrimmerUtil videoTrimmerUtil;
    private String state;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_clip_video;
    }

    @Override
    protected void initData() {
        super.initData();
        videoTrimmerUtil = new VideoTrimmerUtil();
        path = getIntent().getStringExtra("path");
        if (mDataBinding.trimmerView != null) {

            mDataBinding.trimmerView.setOnTrimVideoListener(this);
            mDataBinding.trimmerView.initVideoByURI(Uri.parse(path));
        }

    }


    private void changeDBL(String url) {
        // String newFBLPath = "/storage/emulated/0/newFBLVideo.mp4";
        String newFBLPath = StorageUtil.getCacheDir() + "/" + System.currentTimeMillis() + getExtensionName(url);
        state = "压缩";
        videoTrimmerUtil.changeVideoFBL(url, newFBLPath, mVideoChangeListener);
    }

    @Override
    public void selectTimeSuccess(String oldPath, String newPath, String startTime, String endTime) {
        //进行视频的裁剪
        state = "裁剪";
        //Log.e("PPS", " startTime " + startTime + " ==== " + endTime);
        videoTrimmerUtil.clipVideo(oldPath, newPath, startTime, endTime, mVideoChangeListener);
    }

    @Override
    public void onCancel() {
        mDataBinding.trimmerView.onDestroy();
        finish();
    }


    private ProgressDialog buildDialog(String msg) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(this, "", msg);
        }
        mProgressDialog.setMessage(msg);
        return mProgressDialog;
    }


    private VideoChangeState mVideoChangeListener = new VideoChangeState() {
        @Override
        public void onStartChange() {
            runOnUiThread(() -> {
                buildDialog("开始" + state).show();
            });

        }

        @Override
        public void onChanging(int progress) {
            runOnUiThread(() -> {
                buildDialog("正在" + state + progress + "%").show();
            });

        }

        @Override
        public void onFinishChange(String url) {
            runOnUiThread(() -> {
                buildDialog(state + "完成").show();
            });
            if (state.equals("裁剪")) {
                runOnUiThread(() -> {
                    changeDBL(url);
                });
            } else {
                //Log.e("PPS","压缩的路径 = " +url);
                Intent intent = new Intent();
                intent.putExtra("path", url);
                setResult(0x124, intent);
                finish();
            }
        }

    };

    /*
     * Java文件操作 获取文件扩展名
     * */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot);
            }
        }
        return filename;
    }
}